import {defineStore} from "pinia"

/**
 * 浏览器信息
 */
function getBrowser() {
    const {clientHeight, clientWidth} = document.documentElement || window.document.documentElement

    const ua = navigator.userAgent.toLowerCase() //浏览器信息

    let type = (ua.match(/firefox|chrome|safari|opera/g) || "other")[0] // 浏览器类型

    if ((ua.match(/msie|trident/g) || [])[0]) {
        type = "msie"
    }

    let tag: any // 平台标签

    const isTouch = "ontouchstart" in window || ua.indexOf("touch") !== -1 || ua.indexOf("mobile") !== -1

    if (isTouch) {
        if (ua.indexOf("ipad") !== -1) {
            tag = "pad"
        } else if (ua.indexOf("mobile") !== -1) {
            tag = "mobile"
        } else if (ua.indexOf("android") !== -1) {
            tag = "androidPad"
        } else {
            tag = "pc"
        }
    } else {
        tag = "pc"
    }

    let prefix: any // 浏览器内核
    switch (type) {
        case "chrome":
        case "safari":
        case "mobile":
            prefix = "webkit"
            break
        case "msie":
            prefix = "ms"
            break
        case "firefox":
            prefix = "Moz"
            break
        case "opera":
            prefix = "O"
            break
        default:
            prefix = "webkit"
            break
    }

    const plat = ua.indexOf("android") > 0 ? "android" : navigator.platform.toLowerCase() // 操作平台

    let screen: any //屏幕信息
    if (clientWidth < 768) {
        screen = "xs"
    } else if (clientWidth < 992) {
        screen = "sm"
    } else if (clientWidth < 1200) {
        screen = "md"
    } else if (clientWidth < 1920) {
        screen = "xl"
    } else {
        screen = "full"
    }
    const isIOS = !!navigator.userAgent.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/) // 是否 ios

    const version = (ua.match(/[\s\S]+(?:rv|it|ra|ie)[\/: ]([\d.]+)/) || [])[1] // 浏览器版本

    const isPC = tag === "pc" // 是否 PC 端

    const isMobile = !isPC // 是否移动端

    const isMini = screen === "xs" || isMobile // 是否移动端 + 屏幕宽过小

    return {
        height: clientHeight,
        width: clientWidth,
        version,
        type,
        plat,
        tag,
        prefix,
        isMobile,
        isIOS,
        isPC,
        isMini,
        screen
    }
}

let browser = getBrowser()

const app = defineStore('app', {
    state: () => {
        return {
            fold: browser.isMini,//是否折叠
            tagList: JSON.parse((sessionStorage.getItem('tagList')) as string) || [],
            browser: browser,
            tag:''
        }
    },
    actions: {
        setTagList(tags: any) {
            sessionStorage.setItem('tagList', JSON.stringify(tags))
            this.tagList = tags
        },
        setTag(e:any){
            this.tag = e
        },
        upTagList(path:string,title:string,q:any,p:any){
            this.tagList.forEach((e:any)=>{
                if (e.path == path){
                    e.title = title
                    e.query = q
                    e.params = p
                }
            })
            sessionStorage.setItem('tagList',JSON.stringify(this.tagList))
        }
    }
})

window.onresize = () => {
    browser = getBrowser()
    if (browser.isMini != app().fold) {
        app().fold = browser.isMini
    }
}
export default app